import { Asset, AudioClip, AudioSource, Component, EventKeyboard, Input, KeyCode, Node, Prefab, Tween, Vec3, _decorator, error, input, instantiate, randomRangeInt, resources } from "cc";

const { ccclass, property } = _decorator;

const move_size: number = 5;

@ccclass("Main")
export class Main extends Component {

    @property(Node)
    SmallHouse: Node;

    @property(Node)
    items: Node;

    private _clips: AudioClip[] = [];


    protected start(): void {

        console.log("数据并入");

        // resources.load('item', (error: Error, data: Asset) => {
        //     if (error) {
        //         console.log('报错了 停止运行')
        //         return;
        //     }

        //     let prefabObject = data as Prefab;

        //     for (let index = 0; index < 10; index++) {

        //         let prefabComponent = instantiate(prefabObject);
        //         this.items.addChild(prefabComponent);

        //         prefabComponent.setPosition(
        //             randomRangeInt(-300, 300),
        //             randomRangeInt(-100, 100)
        //         )

        //     }
        // })

        let audioSourceComponent = this.node.getComponent(AudioSource);

        resources.load('background', (error: Error, data: Asset) => {
            if (error) {
                console.log('error exit;')
                return;
            }

            console.log('播放音乐')

            // 1.直接使用方法
            let audioClip = data as AudioClip;
            // audioSourceComponent.playOneShot(
            //     audioClip, 1
            // )

            // 2.改变属性直接操作
            audioSourceComponent.clip = audioClip;
            audioSourceComponent.loop = true;
            audioSourceComponent.volume = 0.8;
            audioSourceComponent.play();

            // 3.预存起来，下次直接用
            this._clips.push(audioClip);

        })

        if (this._clips[0]) {
            audioSourceComponent.clip = this._clips[0];
            audioSourceComponent.loop = true;
            audioSourceComponent.volume = 0.8;
            audioSourceComponent.play();
        }



    }

    protected update(dt: number): void {

    }

    protected onEnable(): void {
        // this.SmallHouse.on(Node.EventType.TOUCH_START, this.onHouseTouchNodeStart, this)
        input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this)
    }

    protected onDestroy(): void {
        // this.SmallHouse.off(Node.EventType.TOUCH_START, this.onHouseTouchNodeStart, this)
        input.off(Input.EventType.KEY_DOWN, this.onKeyDown, this)
    }

    onKeyDown(event: EventKeyboard) {
        switch (event.keyCode) {
            case KeyCode.KEY_A:
                this.SmallHouse.setPosition(new Vec3(
                    this.SmallHouse.position.x - move_size,
                    this.SmallHouse.position.y,
                    0
                ))
                break;
            case KeyCode.KEY_D:
                this.SmallHouse.setPosition(new Vec3(
                    this.SmallHouse.position.x + move_size,
                    this.SmallHouse.position.y,
                    0
                ))
                break;
            case KeyCode.KEY_W:
                this.SmallHouse.setPosition(new Vec3(
                    this.SmallHouse.position.x,
                    this.SmallHouse.position.y + move_size,
                    0
                ))
                break;
            case KeyCode.KEY_S:
                this.SmallHouse.setPosition(new Vec3(
                    this.SmallHouse.position.x,
                    this.SmallHouse.position.y - move_size,
                    0
                ))
                break;
        }
    }

    onHouseTouchNodeStart() {
        console.log("触发")
    }

}